home *** CD-ROM | disk | FTP | other *** search
- Path: news.halcyon.com!usenet
- From: normanb@halcyon.com (Norm Bryar)
- Newsgroups: comp.lang.c++
- Subject: Re: Coding Standards
- Date: Sun, 10 Mar 1996 17:19:26 GMT
- Organization: Northwest Nexus Inc.
- Message-ID: <4hv2tm$e93@news.halcyon.com>
- References: <4hj8ek$elu@sam.inforamp.net> <4hjh5c$elk@flood.weeg.uiowa.edu>
- NNTP-Posting-Host: blv-pm10-ip17.halcyon.com
- X-Newsreader: Forte Free Agent 1.0.82
-
- Comments with <=== appended
-
- maclenna@ozone.uiowa.edu (Mark MacLennan) wrote:
-
- >In article <4hj8ek$elu@sam.inforamp.net>
- >rmorin@inforamp.net (Randy Charles Morin) writes:
- >>The company I just started a contract with gave me a set of C++ coding
- >>standards. I started reading and couldn't stop laughing. I can't reproduce
- >>them word-for-word, because that wouldn't be nice to the authors and might be
- >>considered a copyright violation or privacy violation, but I'll outline some
- >>of the points.
-
- >Some of their coding standards requirements you list are actually
- >quite good, some are more dubious. Most coding standards are intended
- >as guidelines to be followed unless common sense dicticates otherwise
- >(and these deviations are documented accordingly). You should probably
- >discuss this with the company, inquire as to the intent of some of
- >the standards, rather than laugh behind their back.
-
- >Some of the items you list reveal your own unfamiliarity with C++,
- >which is perhaps one reason they have defined a set of C++ coding
- >standards. (Hopefully the company isn't paying you based on spelling ...)
-
- >- MARK
-
- >>
- >> -source files should have the extension .cc (not .cpp or .c).
- >> -header files should have the extension .hh (not .hpp or .h).
- >> -inline C++ functions should be in a file with the extension .icc
- >> (not in the primary header).
-
- >The latter is a little unusual but no big deal. They can always
- >edit things how they like later.
-
- >> -do not use the /* */ comment, except when commenting out entire
- >>sections of code. My understanding is that /* */ comments are ANSI comments,
- >>while // comments are not ANSI.
-
- >Your understanding is wrong. Both forms are acceptable.
-
- >> -a class which can be instantiated with a "new" must have a copy
- >>constructor, a destructor and an assignment operator definition. This will
- >>extend the project another month.
-
- >Huh? It isn't that hard to do - maybe the company is planning ahead
- >and anticipating the re-use of these classes by other programmers.
-
- >> -never use #define instead or const. What if your concerned about the
- >>mix between code space and data space.
-
- >Not bad advice. Pre-processor directives should be avoided if possible.
- >Better error checking too.
-
- Except you can't put consts in a header for everyone to use and you
- can't bitwise-OR enum values. #defines still have a place. <===
-
- >> -variable are to be declared with the smallest possible scope.
- >> void c::f()
- >> {
- >> {
- >> int b;
- >> {
- >> int a;
- >> a=b+c;
- >> b=a+d;
- >> }
- >> e=b+d;
- >> }
- >> f=e+4;
- >> }
- >> this type of optimization could take forever.
-
- >Use common sense when applying rules such as this. In it's strictest
- >form it may not be appropriate. It is a good idea to keep variables
- >local to where they are actually used.
-
- >> -don't use explicit type conversions. In other words, don't program
- >>in windows. Explicit type conversion is necessary to convert GDI object
- >>handles.
- >> -don't use implicit type conversions implicitly, use them explicitly.
- >> Hello!
-
- >These standards are a little odd but that may be how you worded them.
- >Avoiding implicit type conversions is not such a back idea (use casts
- >to explicitedly show the conversion).
-
- Yes, in Windows programming, type-casts are a way of life and they
- shouldn't be implicit and usually can't be implicit if you've #define
- STRICT like one should. <===
-
- BTW, I guess C++ is moving towards phasing-out C-style type-casts in
- favor of static_cast<>, dynamic_cast<>, reinterpret_cast<>, etc. <==
-
- >> -every case statement must be terminated with a break statement. What
- >>if you want to step through more then one case statement?
-
- >Simply document the exception to the rule ...
-
- Often you want to return instead of break. WndProc's come to mind.
- That's a rule itching to be broken and, it seems to me, otherwise so
- obvious as to not deserve a 'rule'. <===
-
- >> case ENGINE_NOT_ACTIVE:
- >> start_engine();
- >> case ENGINE_ACTIVE_NOT_IN_DRIVE:
- >> shift_to_drive();
- >> case ENGINE_ACTIVE_IN DRIVE:
- >> press_accelerator();
- >> break;
-
- >> -don't use conditional compilation preprocessor directives. There
- >>goes cross-platform development.
-
- >Huh? Since these are pre-processor commands, simply run your code
- >through the pre-processor for the particular platform your client wants
- >the software for. What's the big deal from your point-of-view? Out-of-
- >curiousity, I would inquire as to why they have this programming standard.
-
- This is a ludicrous standard. We always have preprocessor
- conditionals in our code, that's how you get diagnostics in _DEBUG,
- profiling in PROFILE, and neither in NDEBUG. That's how you switch
- endian for your MAC port, etc.
- Anyone seen how assert() is implemented? <===
-
- >> -optimize code only when you have a problem. So we can't optimize
- >>size in order to anticipate that code size will be a problem. First we have
- >>to experience the problem (most likely in the field).
-
- >Perhaps they want well-written code that they and other
- >future programmers can read rather than tricky "optimized" code.
- >The code can always be tweaked for specific hardware at a later
- >time. There is no reason that you can't write efficient code using
- >appropriate algorithms and data structures.
-
- >> -access functions are to be inline. Inline access functions defeat
- >>the purpose of having access functions.
-
- >This isn't clear. Having access functions inplemented inline isn't
- >such a bad idea.
-
- Inline may just mean in the header instead of in the soucefile; inline
- and virtual are incompatible, so in-lining is sacrificed in that case.
- <==
-
- >> -give protected accessors for all data members. What if we need to
- >>make the accessor public. Should we define one protected and one public.
-
- >Accessor functions aren't "data members". Keeping your data isolated
- >to a particular class and only accessible through accessor functions
- >is a good idea.
-
- They must mean accessors to all data-members, protected accessors if
- only sub-classes will need the data, public otherwise. <====
-
- >> -++ and -- operations should be on a separate line.
- >> for (i=0; i<0;
- >> i++
- >> );
- >> Is that better?
-
- >Since the ";" is a statement separator and essentially equivalent to
- >their intent of "separate lines" you needn't do the above. You seem
- >to miss the point of this standard - they want to avoid problems with
- >side effects.
-
- >> -don't allocate memory and expected someone else will delete it later.
- >>Can't use OWL, because control classes are automatically deleted for you.
-
- >This is very good advice - obviously it doesn't apply for packages such
- >as OWL which do it for you.
-
- So, how many spaces are in a tab? Do you leave tabs in or convert
- them to spaces? How many columns long can a line be? Do you have to
- use Hungarian? COM-interfaces? Any limits on how many lines or
- levels of nesting in a function?
-
- I've seen a lot of standards urged on developers.
-
- --Norm
-
-